<?php
namespace My\Controller;

use Utuo\BackgroundController;

class VoteController extends BackgroundController {
	
	private $selectTypes = array('0'=>'单选','1'=>'多选');
	private  $siteUrl="http://w.wmfl.cn";
	
	public function _initialize() {
		$this->setControllerTitle("投票管理");
		$this->setActionTitle(array(
			'index' => '投票表单列表',
			'add' => '添加投票表单',
			'modify' => '修改投票表单',
			'recycle'=>'回收站'	
		));
		$this->setActionNav("index,add,recycle");
	}
	
	public function getLists( $is_deleted=0 ) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$formId = I('id', 0, "intval");
		$voteLists = M('FormVote')->where("cid=%d and is_deleted={$is_deleted}", $this->aid)->limit($pagesize)->page($page)->select();
		foreach ($voteLists as &$v)
		{
			$v['url']=$this->siteUrl.U('Home/Vote/show',array('sid'=>$this->aid,'id'=>$v['id']));
		}
		$voteCount = M("FormVote")->where("cid=%d and is_deleted={$is_deleted}", $this->aid)->count();
		
		$this->showPage($voteCount, $pagesize);
		$this->assign("voteLists", $voteLists);
		$this->display();
	}
	
	/**
	 * 添加投票表单
	 */
	public function addAction() {
		C('TOKEN_ON', true);
		$vote = array();
		$vote['selectOptionsRadio'] = $this->getRadio("select_options", $this->selectTypes, 0);
		$this->assign("vote", $vote);
		$this->display('save');
	}
	
	/**
	 * 修改投票表单
	 */
	public function modifyAction() {
		$id = I('id', 0, 'intval');
		$vote = D("FormVote")->getOneById($id);
		$vote['answer'] = M('FormVoteItem')->where('form_vote_id=%d', $id)->order("sort")->select();
		$vote['selectOptionsRadio'] = $this->getRadio("select_options", $this->selectTypes, $vote['select_options']);
		$this->assign("vote", $vote);
		$this->display('save');
	}
	
	/**
	 * 保存投票表单
	 */
	public function saveAction() {
		false !== D("FormVote")->submit() and $this->ajaxSuccess("保存成功");
		$this->ajaxError("保存失败".D("FormVote")->getError());
	}
	// 移除
	public function removeAction() {
		$this->ajaxRemove("FormVote");
	}
	// 删除
	public function deleteAction() {
		$this->ajaxDelete("FormVote");
	}
	
	// 恢复
	public function restoreAction() {
		$this->ajaxRestore("FormVote");
	}
}